package com.czxy.controller;

import com.czxy.common.vo.Result;
import com.czxy.pojo.TAnswer;
import com.czxy.pojo.TQuestion;
import com.czxy.service.AnswerService;
import com.czxy.vo.AnswerVo;
import lombok.extern.slf4j.Slf4j;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author likk
 * @create 2018/9/5
 */
@RestController
@RequestMapping("/answer")
@Slf4j
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    /**
     * 提交答案
     *
     * @return
     */
    @RequestMapping("/submit")
    public Result answer(TAnswer answer) {

        try {
            answerService.insert(answer);

            return Result.build(200, "新增成功！");
        } catch (Exception e) {
            log.error("新增题目信息失败,错误信息：" + e.getMessage());

            return Result.build(500, "新增失败！");
        }
    }

    /**
     * 查询答题结果
     *
     * @param id
     * @return
     */
    @RequestMapping("/result")
    public Result queryResult(@PathVariable("id") Integer questionId) {
        try {

            // TODO 获取用户信息
            Integer userId = 1;
            TAnswer answer = answerService.findAnswer(questionId, userId);

            return Result.ok(answer);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 阅卷
     *
     * @param answer
     * @return
     */
    @RequestMapping(value = "readOver", method = RequestMethod.POST)
    public Result readOver(@RequestBody AnswerVo answerVo) {
        try {

            // TODO 获取用户信息
            Integer userId = 1;

            answerService.readOver(answerVo, userId);

            return Result.build(200, "修改成功！");
        } catch (Exception e) {
            log.error("更新题目信息失败,错误信息：" + e.getMessage());

            return Result.build(500, "修改失败！");
        }
    }
}
